<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-row :gutter="10">
        <a-col :span="24">
          <a-form-item label="视频名称" field="name">
            <a-input v-model="formData.name" placeholder="请输入视频名称" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import API_LIVE from '@/api/live/live'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()

let title = computed(() => {
  return '视频编辑'
})

// 表单信息
const formData = reactive({
  record_id: null,
  name: '',
})

// 验证规则
const rules = {
  name: [{ required: true, message: '视频名称必需填写' }],
}

// 打开弹框
const open = async (data) => {
  visible.value = true
  formData.record_id = data.record_id
  formData.name = data.name
}


// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = await API_LIVE.editVideo(formData)
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => {
  visible.value = false
  formData = {
    record_id: null,
    name: '',
  }
}

defineExpose({ open})
</script>
